package com.smartthinking.destinomusical.video;

import java.io.IOException;
import java.util.ArrayList;

import android.content.Intent;
import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;

import com.smartthinking.destinomusical.MenuCompartir;
import com.smartthinking.destinomusical.R;
import com.smartthinking.destinomusical.base.BaseActivity;
import com.smartthinking.destinomusical.camara.CameraPreview;
import com.smartthinking.destinomusical.interfaces.WebPlayerInterface;
import com.smartthinking.destinomusical.obj.Cancion;

public class Reproductor extends BaseActivity implements WebPlayerInterface{

	HTML5WebView mWebView;
	int camera, recording;
	Button cameraButton;
	final int RECORDING = 1, STOPPED = 0, BACK_CAMERA = 0, FRONT_CAMERA = 1;
	FrameLayout web, cpf;
	int cameraId;
	CameraPreview cp;
	Cancion cancion;
	
	// private final int delayTime = 90000;
	// private Handler myHandler;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		web = null;
		mWebView = null;
		mWebView = new HTML5WebView(this);
		
		cancion = getIntent().getParcelableExtra("cancion");
		ArrayList<Cancion> cancionesObj = getIntent().getParcelableArrayListExtra("canciones");
		cp = new CameraPreview(this, null, 0); // back camera
		
		String preview ="";
		if (usuario.getSuscripcion().equals("1") || cancion.getCosto().equals("0.00")) {
			preview += "&prev=" + 0;
		} else {
			preview += "&prev=" + 1;
		}
		String url = "http://consola.k-box.com/test2.jsp?id=%s"+preview;
		
		
		for(Cancion song:cancionesObj){
			mWebView.urls.add(String.format(url, song.getUrlId()));
		}
		url = String.format(url, getIntent().getStringExtra("idVideo"));
		mWebView.counter=getIntent().getIntExtra("position", 0);
		mWebView.callback=this;
		
		System.out.println(url);
		if (savedInstanceState != null) {
		    mWebView.restoreState(savedInstanceState);
		} else {
			mWebView.loadUrl(url);
		}

		setContentView(R.layout.videos);
		web = (FrameLayout) findViewById(R.id.web_video);
		cpf = (FrameLayout) findViewById(R.id.my_video);
		cpf.addView(cp);
		web.addView(mWebView.getLayout());
		// cpf.addView(myVideo);
		
		cameraButton = (Button) findViewById(R.id.camara);
		if (Camera.getNumberOfCameras() == 1)
			cameraButton.setEnabled(false);
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		mWebView.saveState(outState);
	}

	@Override
	public void onStop() {
		super.onStop();
		mWebView.stopLoading();

	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		System.out.println("Destroy reproductor");
		mWebView.stopLoading();
		mWebView.goBack();
		mWebView.loadUrl("");
		mWebView.removeAllViews();
		mWebView.clearCache(true);
		web.removeAllViews();

	}

	public void onClickAtras(View vista) {
		if (recording == RECORDING) {
			stopRecording();
		} else {
			if (mWebView.inCustomView()) {
				mWebView.hideCustomView();
				mWebView.goBack();
			}
			finish();
		}
	}
	
	public void onClickCompartir(View w) {
		Intent intent = new Intent(Reproductor.this, MenuCompartir.class);
		intent.putExtra("cancion", cancion);
		startActivity(intent);
	}

	public void onClickCamara(View vista) throws IOException {

		if (camera == BACK_CAMERA) {
			//cameraButton.setText(getString(R.string.boton_camera_b));
			camera = FRONT_CAMERA;
			cameraId = CameraInfo.CAMERA_FACING_FRONT;
		} else {
			//cameraButton.setText(getString(R.string.boton_camera_f));
			camera = BACK_CAMERA;
			cameraId = CameraInfo.CAMERA_FACING_BACK;

		}
		cp.restartPreview(cameraId);
		cpf.removeAllViews();
		cpf.addView(cp);
	}

	public void onClickRecord(View vista) throws IOException {
		if (recording == STOPPED) {
			//((Button) findViewById(R.id.rec)).setText(getString(R.string.boton_camera_parar));
			((Button) findViewById(R.id.rec)).setBackgroundDrawable(getResources().getDrawable(R.drawable.video_des));
			recording = RECORDING;
			cameraButton.setEnabled(false);
			cp.startRecording();
		} else {
			stopRecording();
		}
	}

	public void stopRecording() {
		recording = STOPPED;
		//((Button) findViewById(R.id.rec)).setText(R.string.boton_camera_grabar);
		((Button) findViewById(R.id.rec)).setBackgroundDrawable(getResources().getDrawable(R.drawable.video));
		if (Camera.getNumberOfCameras() > 1)
			cameraButton.setEnabled(true);
		cp.stopRecording();
		/*cp= new CameraPreview(this, null, cameraId);
		cpf.removeAllViews();
		cpf.addView(cp);*/
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {

		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (mWebView.inCustomView()) {
				mWebView.hideCustomView();
				mWebView.goBack();
				return true;
			}

		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public void stop() {
		onKeyDown(KeyEvent.KEYCODE_BACK, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK));
		if (recording == RECORDING) {
			stopRecording();
		} 
		finish();
	}
	
}
